package com.event;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.mysqld.Mysqld;
import com.window.*;

import javax.swing.*;

public class FunctionEvent implements ActionListener {
    public static select select;
    JButton jbutton;

    @Override
    public void actionPerformed(ActionEvent e) {
        Boolean man = Manage.Man.isSelected();
        Boolean woman = Manage.Man.isSelected();
        Boolean allsex = Manage.allSex.isSelected();
        jbutton = (JButton) e.getSource();

        if (jbutton.getName() == "add") {
            if (Manage.nametext.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "姓名不能为空!", "添加页面", JOptionPane.WARNING_MESSAGE);
            } else if ((man == false && woman == false) && allsex == true) {
                JOptionPane.showMessageDialog(null, "“全部”在添加信息时不能被选!", "添加页面", JOptionPane.WARNING_MESSAGE);
            } else if (man == true && woman == false && allsex == false) {
                JOptionPane.showMessageDialog(null, "请选择性别!", "添加页面", JOptionPane.WARNING_MESSAGE);
            } else if (Manage.agetext.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "年龄不能为空!", "添加页面", JOptionPane.WARNING_MESSAGE);
            } else if (Manage.gradetext.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "班级不能为空!", "添加页面", JOptionPane.WARNING_MESSAGE);
            } else if (Manage.numbertext.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "学号不能为空!", "添加页面", JOptionPane.WARNING_MESSAGE);
            } else {
                try {
                    if (Manage.Man.isSelected() == true) {
                        Mysqld.addStudent("男");
                        Mysqld.show1StuInfo();
                    } else {
                        Mysqld.addStudent("女");
                        Mysqld.show1StuInfo();
                    }
                } catch (Exception e1) {
                }
            }
        }

        if (jbutton.getName().equals("del")) {
            try {
                if (Manage.numberconditiontext.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "请输入学号!", "删除页面", JOptionPane.WARNING_MESSAGE);
                } else if (Mysqld.selectnumber()) {
                    Mysqld.delStudent();
                } else {
                    JOptionPane.showMessageDialog(null, "学号不存在!", "删除页面", JOptionPane.WARNING_MESSAGE);
                }
            } catch (Exception e1) {
            }
        }

        if (jbutton.getName().equals("change")) {
            try {
                if (Manage.numberconditiontext.getText().equals("")) {
                    JOptionPane.showMessageDialog(null, "请输入要更改的学号!", "修改页面", JOptionPane.WARNING_MESSAGE);
                } else if (Mysqld.selectnumber()) {
                    if (Manage.nametext.getText().equals("")) {
                        JOptionPane.showMessageDialog(null, "姓名不能为空!", "添加页面", JOptionPane.WARNING_MESSAGE);
                    } else if ((man == false && woman == false) && allsex == true) {
                        JOptionPane.showMessageDialog(null, "“全部”在修改信息时不能被选!", "添加页面", JOptionPane.WARNING_MESSAGE);
                    } else if (man == true && woman == false && allsex == false) {
                        JOptionPane.showMessageDialog(null, "请选择性别!", "添加页面", JOptionPane.WARNING_MESSAGE);
                    } else if (Manage.agetext.getText().equals("")) {
                        JOptionPane.showMessageDialog(null, "年龄不能为空!", "添加页面", JOptionPane.WARNING_MESSAGE);
                    } else if (Manage.gradetext.getText().equals("")) {
                        JOptionPane.showMessageDialog(null, "班级不能为空!", "添加页面", JOptionPane.WARNING_MESSAGE);
                    } else if (Manage.numbertext.getText().equals("")) {
                        JOptionPane.showMessageDialog(null, "学号不能为空!", "添加页面", JOptionPane.WARNING_MESSAGE);
                    } else {
                        if (Manage.Man.isSelected() == true) {
                            Mysqld.changeStuInfo("男");
                            Mysqld.show1StuInfo();
                        } else {
                            Mysqld.changeStuInfo("女");
                            Mysqld.show1StuInfo();
                        }
                    }

                } else {
                    JOptionPane.showMessageDialog(null, "学号不存在!", "修改页面", JOptionPane.WARNING_MESSAGE);
                }
            } catch (Exception e1) {

            }
        }


        if (jbutton.getName().equals("show")) {
            select a=new select();
            select=a;
        }

        if (jbutton.getName().equals("reset")) {
            Manage.nametext.setText("");
            Manage.agetext.setText("");
            Manage.gradetext.setText("");
            Manage.numbertext.setText("");
            Manage.numberconditiontext.setText("");
            Manage.Man.setSelected(false);
            Manage.Woman.setSelected(false);
            Manage.allSex.setSelected(false);

        }


    }
}


